<?php

class OOWP_Element_DynamicMarkup
    extends OOWP_Element
{

    public function __construct(array $args = array( ))
    {
        $input = $this->merge_arguments(array(
            'anchor_arg' => null,
            'anchor_mode' => null,
            'content_arg' => 'post_title',
            'content_mode' => 'post',
            'pre_anchor_filters' => null,
            'post_anchor_filters' => null,
        ), $args);

        parent::__construct($input);
    }

    /* ACCESSORS */

    protected function getAnchorArg( )
    {
        return $this->dict['anchor_arg'];
    }

    protected function setAnchorArg($anchor_arg)
    {
        if (is_null($anchor_arg) || is_string($anchor_arg)) {
            $this->dict['anchor_arg'] = $anchor_arg;
        }
    }

    protected function getAnchorMode( )
    {
        return $this->dict['anchor_mode'];
    }

    protected function setAnchorMode($anchor_mode)
    {
        if (in_array($anchor_mode, array(
            null,
            'home',
            'manual',
            'post',
            ))) {
            $this->dict['anchor_mode'] = $anchor_mode;
        }
    }

    protected function getContentArg( )
    {
        return $this->dict['content_arg'];
    }

    protected function setContentArg($content_arg)
    {
        if (is_null($content_arg) || is_string($content_arg)) {
            $this->dict['content_arg'] = $content_arg;
        }
    }

    protected function getPostAnchorFilters( )
    {
        return $this->dict['post_anchor_filters'];
    }

    protected function setPostAnchorFilters($filters)
    {
        $this->dict['post_anchor_filters'] = $filters;
    }

    protected function getPreAnchorFilters( )
    {
        return $this->dict['pre_anchor_filters'];
    }

    protected function setPreAnchorFilters($filters)
    {
        $this->dict['pre_anchor_filters'] = $filters;
    }

    protected function getContentMode( )
    {
        return $this->dict['content_mode'];
    }

    protected function setContentMode($content_mode)
    {
        if (in_array($content_mode, array(
            null,
            'bloginfo',
            'lambda',
            'post',
            'post_meta',
            'theme_option'))) {
            $this->dict['content_mode'] = $content_mode;
        }
    }

    protected function getGovernor( )
    {
        $governor = $this->parent;
        if (is_null($this->parent)) { echo '<p>NULL</p>'; }
        while(!is_null($governor)) {
            if ($governor instanceof OOWP_Element_Article) {
                return $governor;
            } else {
                $governor = $governor->parent;
            }
        }
        return $governor;
    }

    /* METHODS */

    protected function filter_contents($pieces)
    {
        switch ($this->content_mode) {
            case 'bloginfo':
                $content = get_bloginfo($this->content_arg);
                break;
            case 'lambda':
                $content = $this->content_arg( );
                break;
            case 'post':
                if (!is_null($this->governor)) {
                    if (!is_null($this->governor->post)) {
                        $post = $this->governor->post;
                        $arg = $this->content_arg;
                        $content = $post->$arg;
                    } else {
                        $content = null;
                    }
                } else {
                    $content = null;
                }
                break;
            case 'post_meta':
                if (!is_null($this->governor) && $this->governor->post) {
                    $post = $this->governor->post;
                    $content = get_post_meta($post->ID, $this->content_arg, true);
                } else {
                    $content = null;
                }
                break;
            case 'theme_option':
                $content = get_option($this->content_arg, '');
                break;
            case null:
            default:
                $content = null;
        }

        if (!is_null($this->pre_anchor_filters)) {
            foreach ($this->pre_anchor_filters as $filter) {
                $content = $filter->filter($content, $this);
            }
        }

        switch ($this->anchor_mode) {
            case 'home':
                $content = '<a href="' . get_bloginfo('url') . '">' . $content . '</a>';
                break;
            case 'manual':
                $content = '<a href="' . $this->anchor_arg . '">' . $content . '</a>';
                break;
            case 'post':
                if (is_null($this->anchor_arg)) {
                    if (!is_null($this->governor) && $this->governor->post) {
                        $post = $this->governor->post;
                        $content = '<a href="' . get_permalink($post->ID) . '">' . $content . '</a>';
                    }
                } elseif (is_numeric($this->anchor_arg)) {
                    $content = '<a href="' . get_permalink($this->anchor_arg) . '">' . $content . '</a>';
                }
                break;
            case null:
            default:
                break;
        }

        if (!is_null($this->post_anchor_filters)) {
            foreach ($this->post_anchor_filters as $filter) {
                $content = $filter->filter($content, $this);
            }
        }

        return array($content);
    }

}

